@echo off
cd /d "%~dp0"
set ShowHelp=0
set SIGN_CMD="%~dp0\sign_tool\windows\atb_signer.exe"
set SIGNER_KEY="%~dp0\sign_tool\keys\TestRSA2048_ossl.pem"

if "%1" == "" (set ShowHelp=1 
goto PrintHelp
)

if "%2" == "" (set ShowHelp=1 
goto PrintHelp
)

if "%3" == "" (set ShowHelp=1 
goto PrintHelp
)

@REM Generate an empty.bin file, where the size of the bin file needs to be specified by the user. 
DD\dd.exe if=/dev/zero of=empty.bin bs=256k count=%3

set SIGNED_IMAGE=empty.bin.rsa2048.signed

if exist %SIGNED_IMAGE% del %SIGNED_IMAGE%

@REM dlp = 0x08000000 + offset * 0x200, to/entry address means entryAddress
set core_list=--iib nohash core=0 type=0x0 image=empty.bin dlp=%1 to=%2  entry=%2 --psn 0x01
@REM set core_list=--iib core=0 type=0x0 image=%1 dlp=0x208 to=0x08041000  entry=0x08041000 --psn 0x01
echo %core_list%  ccc
%SIGN_CMD% sign --v 2 --sec_ver 0x1234 --dgst sha256 --rcp key=%SIGNER_KEY% %core_list% --of %SIGNED_IMAGE%

@REM convert bin to hex
.\bin2hex\Bin2Hex.exe -b 0x08000000,%SIGNED_IMAGE% -o empty.hex

@REM Bin2Hex.exe -b 0x10140000,test.bin,0x10340000,test.bin -o out.hex

@REM package
pactool\pactool make_pac_image_no_gpt --output empty.pac ^
--allow_empty_partitions ^
--product E3650 ^
--da FDA:pactool/temp/nvm/FDA.bin ^
--image 0x8000000:%SIGNED_IMAGE%

:PrintHelp
if %ShowHelp% == 1 (echo;
echo Usage: %0 unsigned.bin dlpoffset entryAddress 
echo e.g.   Take e3 board for example, the command is as followed
echo        %0 dlp entryAddress desire size
echo;
)
